package com.indusfo.spc.vo;

import java.util.*;

/**
 * @ProjectName: IEIS2-COM
 * @Package: com.indusfo.spc.vo
 * @ClassName: OrderUtil
 * @Author: 熊冰
 * @Description:  排序工具类
 * @Date: 2019/9/20 9:53
 * @Version: 1.0
 */
public class OrderUtil {

    //上移
    public Map<Integer,Integer> OrederSortUp(Map<Integer,Integer> map,Integer id){
        map= OrederSort(map);
        //当前值
        Integer nowValue=map.get(id);
        //交换id
        Integer changeId = 0;
        //交换值
        Integer changeValue=nowValue-1;
//        System.out.println(nowValue+changeValue);
//        System.out.println(id+changeId);
        if(changeValue<=0){
            System.out.println("无法上移");
            changeValue=1;


        }
        //获取交换id
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            if(mapValue==changeValue){
                changeId=mapKey;
            }
        }
//        System.out.println(nowValue+changeValue);
//        System.out.println(id+changeId);
        map=  OrederChange(map,id,changeId);
        return  map;
    }

    //下移
    public Map<Integer,Integer> OrederSortDown(Map<Integer,Integer> map,Integer id){
        map= OrederSort(map);
        //当前值
        Integer nowValue=map.get(id);
        //交换id
        Integer changeId = null;
        //交换值
        Integer changeValue=nowValue+1;
        if(changeValue>=map.size()+1){
            changeValue=map.size();
            System.out.println("无法下移");
            return  map;
        }
        //获取交换id
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            if(mapValue.equals(changeValue)){
                changeId=mapKey;
            }
        }
        map=  OrederChange(map,id,changeId);
        return  map;
    }

    public Map<Integer,Integer> OrederChange(Map<Integer,Integer> map,Integer nowId,Integer changeId) {
        Integer nowValue=map.get(nowId);
        Integer changeValue=map.get(changeId);

        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            if(mapKey.equals(nowId)){
                map.put(mapKey,changeValue);
            }
            if(mapKey.equals(changeId)){
                map.put(mapKey,nowValue);
            }
//            map.put(mapKey,i++);
//            System.out.println(mapKey+":"+mapValue);
        }
        map = OrederSort(map);
        return  map;
    }

    public Map<Integer,Integer> OrederSort(Map<Integer,Integer> map){

        //去空
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            if(mapKey==null){
                break;
            }
            if(mapValue==null){
                map.put(mapKey,map.size());
            }
//            System.out.println(mapKey+":"+mapValue);
        }
        //排序
        map = sortByValueAscending(map);
        int i=1;
        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
            Integer mapKey = entry.getKey();
            Integer mapValue = entry.getValue();
            map.put(mapKey,i++);
//            System.out.println(mapKey+":"+mapValue);
        }
        //展示
//        for(Map.Entry<Integer, Integer> entry : map.entrySet()){
//            Integer mapKey = entry.getKey();
//            Integer mapValue = entry.getValue();
////            map.put(mapKey,i++);
//            System.out.println(mapKey+":"+mapValue);
//        }
        return  map;
    }

    //降序排序
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueDescending(Map<K, V> map)
    {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>()
        {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
            {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return -compare;
            }
        });

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }
    //升序排序
    public static <K, V extends Comparable<? super V>> Map<K, V> sortByValueAscending(Map<K, V> map)
    {
        List<Map.Entry<K, V>> list = new LinkedList<Map.Entry<K, V>>(map.entrySet());
        Collections.sort(list, new Comparator<Map.Entry<K, V>>()
        {
            @Override
            public int compare(Map.Entry<K, V> o1, Map.Entry<K, V> o2)
            {
                int compare = (o1.getValue()).compareTo(o2.getValue());
                return compare;
            }
        });

        Map<K, V> result = new LinkedHashMap<K, V>();
        for (Map.Entry<K, V> entry : list) {
            result.put(entry.getKey(), entry.getValue());
        }
        return result;
    }



}
